package com.kacheap.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.kacheap.configuration.DynamicScheduledTaskRegistrar;


@Service
public class ScheduledTaskService {
	private static final Logger log = LoggerFactory.getLogger(ScheduledTaskService.class);

	private final DynamicScheduledTaskRegistrar scheduledTaskRegistrar = new DynamicScheduledTaskRegistrar();

	/**
	 * 新增任务
	 * 
	 * @param taskName
	 * @param cron
	 */
	public void add(String taskName, String cron) {
		Boolean result = scheduledTaskRegistrar.addCronTask(taskName, cron, () -> print(taskName));
		log.info("定时任务添加结果：" + result);
	}

	/**
	 * 取消任务
	 * 
	 * @param taskName
	 */
	public void cancel(String taskName) {
		scheduledTaskRegistrar.cancelCronTask(taskName);
	}

	
	/**
	 * 获取所有任务
	 * @return
	 */
	public List<String> tasks() {
		return scheduledTaskRegistrar.tasks();
	}
	
	
	private void print(String taskName) {
		log.info(taskName + " ==> 开始执行");
	}

}